package com.winning.cqs.pregnantbabycare_cqs_v1.activity;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Vector;

import com.contec.jar.contec08a.DeviceCommand;
import com.contec.jar.contec08a.DeviceData;
import com.contec.jar.contec08a.DevicePackManager;

import android.R.integer;
import android.util.Log;


public class BloodBuf {
	private static final String TAG = "com.testBlueTooth.Mtbuf";
	public static Vector<Integer> m_buf = null;

   public static final int e_pack_pressure_back = 0x46;
	BloodBuf() {
		m_buf = new Vector<Integer>();
	}

	public synchronized int Count() {
		return m_buf.size();
	}

	DevicePackManager mPackManager = new DevicePackManager();
	private DeviceData mDeviceData;
	public static final int e_pack_hand_back = 0x48;
	public static final int e_pack_oxygen_back = 0x47;
	private int mType = 0;
	public synchronized void write(byte[] buf, int count,
			OutputStream pOutputStream) throws Exception {

		int state = mPackManager.arrangeMessage(buf, count, mType);
		int x = DevicePackManager.Flag_User;
		switch (state) {
		case e_pack_hand_back:
			switch (mType) {
			case 9:
				mType = 5;
				pOutputStream.write(DeviceCommand.DELETE_BP());
				break;
			case 0:
				mType = 3;
				pOutputStream.write(DeviceCommand.correct_time_notice);
				break;
//			case 1:
//				mType = 2;
//				pOutputStream.write(DeviceCommand.REQUEST_OXYGEN());
//				break;
//			case 7:
//				mType = 8;
//				pOutputStream.write(DeviceCommand.REQUEST_OXYGEN());
//				break;
//			case 2:
//				mType = 5;
//				pOutputStream.write(DeviceCommand.DELETE_OXYGEN());
//				break;
//			case 8:
//				mType = 5;
//				pOutputStream.write(DeviceCommand.DELETE_OXYGEN());
//				break;
			case 3:
				mType = 1;

				if (x == 0x11) {
					mType = 7;// 三个用户
				} else {
					mType = 1;// 单用户
				}

				pOutputStream.write(DeviceCommand.REQUEST_BLOOD_PRESSURE());
				break;
			}
			break;
		case 0x30:// 确认校正时间正确
			pOutputStream.write(DeviceCommand.Correct_Time());
			break;
		case 0x40:// 校正时间正确
			pOutputStream.write(DeviceCommand.REQUEST_HANDSHAKE());
			break;
		case e_pack_pressure_back:
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {////防止最后一条命令血压设备接收不到
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ArrayList<byte[]> _dataList = mPackManager.mDeviceData.mData_blood;
			int _size = _dataList.size();
			DeviceData _mData = new DeviceData();
			for (int i = 0; i < _size; i++) {
				byte[] _byte = _dataList.get(i);
				byte[] _data = new byte[10];
				

				
					int hiPre = ((_data[5] << 8 )|( _data[6] & 0xff)) & 0xffff;// 高压
					int lowPre = _data[7] & 0xff;// 低压
				}
			if (_size == 0) {
				pOutputStream.write(DeviceCommand.REPLAY_CONTEC08A());

				Log.e(TAG, "-------Pressure-------");
				Log.e(TAG, "血压血压血压血压血压");
			
			}else{
				pOutputStream.write(DeviceCommand.REPLAY_CONTEC08A());

			}


			break;
		}
		
	}

}
